package kos.robert.loader;

import java.awt.*;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

import kos.robert.animation.Animation;
import kos.robert.screens.Screen;

public class AnimationLoader {

	public static void main(String[] args) {
		DisplayMode dm = new DisplayMode(1024, 768, 32, DisplayMode.REFRESH_RATE_UNKNOWN);
		AnimationLoader animationLoader = new  AnimationLoader();
		animationLoader.run(dm);
	}

	private Screen screen;
	private Image bg;
	private Animation animation;

	// engine to run
	public void run(DisplayMode dm) {
		screen = new Screen();
		
		try {
			screen.setFullScreen(null, new JFrame());
			loadpics();
			movieLoop();
		} finally {
			screen.restoreScreen();
		}
	}

	// load pictures
	public void loadpics() {
		bg = new ImageIcon("resources/graphics/back.png").getImage();
		Image face1 = new ImageIcon("resources/graphics/face.png").getImage();
		Image face2 = new ImageIcon("resources/graphics/face01.png").getImage();
		animation = new Animation(true);
		animation.addScene(face1, 250);
		animation.addScene(face2, 250);
	}

	public void movieLoop() {
		long startingTime = System.currentTimeMillis();
		long cumTime = startingTime;

		while (cumTime - startingTime < 5000) {
			long timePassed = System.currentTimeMillis() - cumTime;
			cumTime += timePassed;
			animation.update(timePassed);

			Graphics g = screen.getFullScreenWindow().getGraphics();
			draw(g);
			g.dispose();

			try {
				Thread.sleep(20);
			} catch (Exception e) {
			}

		}
	}
	
	public void draw (Graphics g){
		g.drawImage(bg, 0, 0, null);
		g.drawImage(animation.getImage(), 0, 0, null);
	}
}